home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v10n12.arc / PRINTF.C < prev    next >
Text File  |  1991-05-30  |  3KB  |  119 lines

  1. /* 
  2. PRINTF.C -- simple output for small Windows programs, 
  3. using MessageBox() or WinExec()/SendMessage()
  4.  
  5. Copyright (c) 1991 Ziff Communications Co.
  6.     PC Magazine * Andrew Schulman
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include <string.h>
  12. #include <windows.h>
  13. #include "printf.h"
  14.  
  15. #define BUF_SIZE        1024
  16.  
  17. static char *str, *app;
  18. static unsigned cap, len;
  19. static int lines;
  20.  
  21. BOOL open_display(char *appname)
  22. {
  23.     app = appname;
  24.     cap = 128;
  25.     if (! (str = malloc(cap)))
  26.         return FALSE;
  27.     *str = lines = len = 0;
  28.     return TRUE;
  29. }
  30.  
  31. /* Maximum number of lines that MessageBox will hold */
  32. static int max_lines(void)
  33. {
  34.     TEXTMETRIC tm;
  35.     HWND hWnd = GetActiveWindow();
  36.     HDC hDC = GetWindowDC(hWnd);
  37.     if (hDC == NULL)
  38.         return 0;
  39.     GetTextMetrics(hDC, &tm);
  40.     ReleaseDC(hWnd, hDC);
  41.     return (GetSystemMetrics(SM_CYFULLSCREEN) /
  42.         (tm.tmHeight + tm.tmExternalLeading)) - 5;
  43. }
  44.  
  45. BOOL show_display(void)
  46. {
  47.     if (lines <= max_lines())
  48.         MessageBox(NULL, str, app, MB_OK);
  49.     else
  50.         notepad(str);
  51.     free(str);
  52.     return TRUE;
  53. }
  54.  
  55. static BOOL append(char *s2)
  56. {
  57.     char *s3;
  58.     if (((len += strlen(s2)) < cap) && strcat(str, s2))
  59.         return TRUE;
  60.     cap = len + 128;
  61.     if (! (s3 = malloc(cap))) 
  62.         return FALSE;
  63.     strcpy(s3, str);
  64.     strcat(s3, s2);
  65.     free(str);
  66.     str = s3;
  67.     return TRUE;
  68. }
  69.  
  70. int nlines(char *s2)
  71. {
  72.     int c, n = 0;
  73.     while (c = *s2++)
  74.         if (c == '\n') 
  75.             n++;
  76.     return n;
  77. }
  78.  
  79. int printf(const char *fmt, ...)
  80. {
  81.     static char s2[BUF_SIZE];
  82.     int len;
  83.     va_list marker;
  84.     va_start(marker, fmt);
  85.     len = vsprintf(s2, fmt, marker);
  86.     lines += nlines(s2);
  87.     va_end(marker);
  88.     append(s2);
  89.     return len;
  90. }
  91.  
  92. BOOL notepad(char far *s)
  93. {
  94.     HWND notepad;
  95.     HWND edit_ctrl;
  96.     if (WinExec("notepad.exe", SW_SHOWNORMAL) < 32)
  97.         return FALSE;
  98.     notepad = FindWindow(NULL, "Notepad - (untitled)");
  99.     edit_ctrl = GetFocus();
  100.     SendMessage(notepad, WM_SETTEXT, 0, (char far *) app);
  101.     SendMessage(edit_ctrl, WM_SETTEXT, 0, (char far *) s);
  102.     return TRUE;
  103. }
  104.  
  105. #ifdef TESTING
  106. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  107.     LPSTR lpszCmdLine, int nCmdShow)
  108. {
  109.     int i;
  110.     open_display("GetSystemMetrics");
  111.     for (i=0; i<37; i++)
  112.     {
  113.         printf("%d\t%d\r\n", i, GetSystemMetrics(i));
  114.         Yield();
  115.     }
  116.     show_display();
  117. }
  118. #endif
  119.